﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SageKing.Studio.Pages.Base;
using SageKing.Studio.Shared;
using System.Drawing.Drawing2D;

namespace SageKing.Studio.Pages.SageKingMessage.component;

public class ImportFromDBAttPageModal(
    IMessageService ShowMessage,
    SysSageKingMessageService dataService,
    SysSageKingMessageAttributeService dataDetailService

    )
    : ModalCRUDBase<ModelDataBaseInput, ImportFromDBAttPage, long, ModelDataBaseInput>
{

    public PageBase<SysSageKingMessage> PageData { get; set; }

    public Action StateHasChanged { get; set; } = () => { };
    public Func<bool, Task> RefreshFunc { get; set; } = (a) => Task.CompletedTask;


    #region IModalCRUD
    public IModalCRUD<ModelDataBaseInput, ImportFromDBAttPage, long, ModelDataBaseInput> MeCrud => this;

    public string TabTitle { get; set; }
    public string Title { get; set; } = "消息结构-快速导入-数据库";
    public string ParentName { get; set; }

    public string OkTitle { get; set; } = "导入";

    public string CancelText { get; set; } = "取消";

    public bool Visible { get; set; } = false;

    public bool Loading { get; set; } = false;
    public ModelDataBaseInput Model { get; set; }
    public ImportFromDBAttPage Component { get; set; }
    public Error? Error { get; set; }
    public RenderFragment ModalTemplate { get; set; }

    public Task DeleteData(ModelDataBaseInput row, ModelDataBaseInput parent)
    {
        throw new NotImplementedException();
    }

    public Task EditOnStatusSwitch(ModelDataBaseInput row)
    {
        throw new NotImplementedException();
    }

    public async Task HandleOk(MouseEventArgs e)
    {
        try
        {
            Loading = true;
            if (Component != null)
            {
                var result = await Component.Save();
                if (result > 0 || Component.selectTable == null || !Component.colOutputs.HasItem())
                {
                    Visible = true;
                    Loading = false;
                    return;
                }

                SysSageKingMessage sysSageKingMessage = new SysSageKingMessage()
                {
                    Name = Component.selectTable.TableName,
                    Description = Component.selectTable.TableComment,
                    Status = false,
                    Type = 1
                };
                //保存主表  
                var isCount = await dataService.CountAsync(a => a.Name.StartsWith(sysSageKingMessage.Name));
                if (isCount > 0)
                {
                    sysSageKingMessage.Name += isCount + 1;
                }

                var mainId = await dataService.AddReturnId(sysSageKingMessage);

                //保存子表
                var listAttr = Component.colOutputs.Select(a =>
                    new SysSageKingMessageAttribute()
                    {
                        MessageId = mainId,
                        Name = a.ColumnName,
                        Description = a.ColumnComment,
                        Type = a.NetType,
                        DisplayName = a.ColumnName,
                        Status = true
                    }
                );

                var saveAtt = dataDetailService.Add(listAttr);
                //end

            }
            await RefreshFunc(false);
            _ = ShowMessage.SuccessAsync($"{Title}【{Model.TableName}】成功！");
            Visible = false;
            Loading = false;
            Component?.Reset();
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex, $"{Title}【{Model.TableName}】失败！");
        }
    }

    public Task HandleClose(MouseEventArgs e)
    {
        Visible = false;
        Component.Reset();
        return Task.CompletedTask;
    }

    public Task<bool> InitModel(long id)
    {
        throw new NotImplementedException();
    }

    #endregion
}
